<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2021/07/15 9:59
 * Blog：www.myblogs.xyz
 */

namespace app\common\service;


use app\common\model\Setting;

class VerifyService
{
    protected static $error = '';

    public static function card($name, $card)
    {
        if (empty($name)){
            self::$error = '请填写姓名';
            return false;
        }
        if(!preg_match("/[\x7f-\xff]/", $name)){
            self::$error = '请填写真实姓名';
            return false;
        }
        if (empty($card)){
            self::$error = '请填写身份证';
            return false;
        }
        $setting_model = new Setting();
        $setting_data = $setting_model->settingFind('basic');
        if (empty($setting_data)){
            self::$error = '操作失败';
            return false;
        }
        import('card.card');
        $card_class = new \card();
        if (!$card_class->is_card($card)){
            self::$error = '请输入正确的身份证';
            return false;
        }
        $age = $card_class->get_age($card);
        if (intval($age) >= intval($setting_data['min_age']) && intval($age) <= intval($setting_data['max_age'])){
            // 身份证和姓名匹配
            $rows = self::curl($card, $name);
            if ($rows){
                return true;
            }
            self::$error = '姓名或身份证号有误';
            return false;
        }
        self::$error = '允许使用年龄范围' . intval($setting_data['min_age']);
        self::$error .= '-' . intval($setting_data['max_age']) . '周岁';
        return false;
    }

    protected static function curl($card, $name)
    {
        $param = [
            'idcard'=>$card,
            'name'=>$name
        ];
        $host = "https://idcardcheck.shumaidata.com";
        $path = "/idcardcheck";
        $method = "GET";
        $appcode = config('app_code');
        if (empty($appcode)){
            return true;
        }
        $headers = [
            "Authorization:APPCODE " . $appcode
        ];
        $url = $host . $path . "?" . http_build_query($param);

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $result = curl_exec($curl);
        $rows = json_decode($result, true);
        if (!empty($rows['code']) && $rows['code'] == 200){
            $res = empty($rows['data']['result']) ? 0 : $rows['data']['result'];
            if ($res == 0){
                return true;
            }
        }
        return false;
    }

    public static function getError()
    {
        return self::$error;
    }

}